﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using Newtonsoft.Json.Linq;
using ZBZH_Manage_Order.PublicMethod;

namespace ZBZH_Manage_Order.Controllers
{
    public class LoginController : Controller
    {
        ZHHQSERVICE.Service1Client service = new ZHHQSERVICE.Service1Client();

        public ActionResult Index()
        {
            return View();
        }
        public ActionResult LoginPage()
        {          
            return View();
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="acc">账号</param>
        /// <param name="pwd">密码</param>
        /// <param name="role">角色；1：一般用户，2：操作员</param>
        /// <returns></returns>
        public ActionResult Check(string acc, string pwd, string role)
        {
            try
            {
                List<Basic_Dict_T> ll = GetDict.DictList;
                if (role == "1")
                {
                    Session.Timeout = 60;
                    Model.View_User dataresult = new View_User();
                    string result = service.UserLogin(acc, pwd,1, GetYZM._YZM, 1);//调用服务
                    JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                    string bs = searcho["BS"].ToString();
                    if (bs == "1")
                    {
                        IList<JToken> jieguo = searcho["rows"].Children().ToList();//将json数据转成多条
                        dataresult = obser<DataResult>.DataContractJsonDeserialize<Model.View_User>(searcho["rows"].ToString());//将json格式转为实体类
                        Session[SessionStorage.UserLogin] = dataresult;//将实体类保存至Session
                        //Session[SessionStorage.OperUser] = null;
                        Session[SessionStorage.LogMethod] = 1;
                        //Model.User_Login_T vv = (Model.User_Login_T)Session[SessionStorage.UserLogin];//将Session保存的实体转成可直接使用的实体
                        //#region cookie
                        //HttpCookie _cookie = new HttpCookie("User");
                        //_cookie.Values.Add("UserName", acc);
                        //_cookie.Values.Add("Password", pwd);
                        //Response.Cookies.Add(_cookie);
                        //if (Request.QueryString["ReturnUrl"] != null)
                        //{
                        //    return Redirect(Request.QueryString["ReturnUrl"]);
                        //}
                        //else
                        //{
                        //    return RedirectToAction("/Home/Index", "User");
                        //}
                        //#endregion
                    }
                    return Content(bs);
                }
                else
                {
                    Session[SessionStorage.LogMethod] = 2;
                    Model.View_OperUser dataresult = new View_OperUser();
                    string result = service.OperUserLogin(acc, pwd, GetYZM._YZM, 1);
                    JObject searcho = JObject.Parse(result);//将服务返回的结果进行优化
                    string bs = searcho["BS"].ToString();
                    if (bs == "1")
                    {
                        dataresult = obser<DataResult>.DataContractJsonDeserialize<Model.View_OperUser>(searcho["rows"].ToString());//将json格式转为实体类
                        Session[SessionStorage.OperUser] = dataresult;//将实体类保存至Session
                        Session[SessionStorage.UserLogin] = null;
                    }
                    return Content(bs);
                }
            }
            catch (Exception) { return Content("0"); }
            finally
            {
                new ZBZH_Manage_Order.PublicMethod.AppDictionary();  // 加载字典表数据到本地
            }
        }

    }
}
